﻿//09. Write a program to calculate the Nth Catalan number by given N.


using System;
using System.Numerics;

class CatalansNumbeer
{
    static void Main()
    {
        bool parseSuccess;
        uint inputValueN; //UInt64.MaxValue = 18 446 744 073 709 551 615 (2^64-1)
        do
        {
            Console.Write("Input number N >= 0: ");
            string strA = Console.ReadLine();
            parseSuccess = UInt32.TryParse(strA, out inputValueN);
        }
        while ((inputValueN < 0) || (inputValueN > UInt32.MaxValue) || (!parseSuccess)); // Accept only N >= 0

        if ((inputValueN == 0) || (inputValueN == 1)) //two exeptions
        {
            Console.WriteLine("\nCatalana's Number is = " + 1);
            return;
        }

        //Calculation for numerator==[(N+2) .. to (2*N)]!
        uint k = (inputValueN + 1);//here and next row N+2==k
        k++; //Because the calculation starts from ((K+1) to N)!
        BigInteger factorialNumerator = 1;
        do
        {
            factorialNumerator *= k;
            k++;
        } while (k <= (2 * inputValueN));     

        //Calculation for N! in divisor
        BigInteger factorialN = 1;
        do //calculate * N!
        {
            factorialN *= inputValueN;
            inputValueN--;
        } while (inputValueN > 0);
       
        Console.WriteLine("\nCatalana's Number is = " + (factorialNumerator / factorialN));
        Console.WriteLine();
    }
}